<?php

class Login extends Model {


    public function getPhoto($unit,$grid_unit){

        $site = HTTP_FRONTEND;
        $img_path = $site . "/Image/$unit" ."/";

        $totalItemsPerSearch = 0;
        $i = 0;
        $pagesCount = 0;
        $result = array();
        $list = array();

        $items_per_page = $this->registry->request->getParameter('rows');
        if(is_null($items_per_page)) $items_per_page = 15;

        $pagenum = $this->registry->request->getParameter('page');
        if(is_null($pagenum)) $pagenum = 1;



        $column_models = $this->getColumnModel($this->registry->modul_index, $grid_unit);

        $photos = $this->furnitureProcess();

        if(isset($photos['count'])) $totalItemsPerSearch = $photos['whole_count'];
        if(isset($photos['data']))  $list = $photos['data'];

        if($totalItemsPerSearch > 0)
            foreach($list as $k => $photo){

                $link = '';
                if(isset($photo['link'])) $link = $photo['link'];
                if(isset($photo['link_sm'])) $link = $photo['link_sm'];

                $cell = array();

                foreach ($column_models as $column) {
                    switch ($column ['name']) {
                        case 'date_added':
                            $cell[] = $photo['date_added'];
                            break;
                        case 'title':
                            $cell[] = $photo['title'];
                            break;
                        case 'text':
                            $cell[] = $photo['text'];
                            break;
                        case 'link':
                            $img = "<img src='$img_path".$link."' class='smm-picto' />";
                            $image = $img_path.$link;
                            $cell[] = $image;
                            //$cell[] = array('src' => $image);
                            //$tmp = array();
                            //$tmp['src'] = $img_path.$link;
                            //$cell[] = $tmp;
                            break;
                        case 'sort':
                            $cell[] = $photo['sort'];
                            break;
                        case 'actions':
                            $switched = 'switched-off';
                            $title = 'Сделать картинку видимой';

                            if(isset($photo['is_active'])){
                                if((int)$photo['is_active'] === 1){
                                    $switched = 'switched-on';
                                    $title = 'Спрятать картинку';
                                }
                            }

                            $cell[] = "<div class='actions delete' onClick='deleteItem(\"photo\",{$photo['photo_id']},\"$grid_unit\", \"$unit\")'></div>".
                                "<div class='actions vis-mode $switched' title='$title' onClick='toggleVisibility(\"photo\",{$photo['photo_id']},\"$grid_unit\", \"$unit\")'></div>"  ;
                            break;
                    }
                }

                $item = array(
                    'id' => $photo['photo_id'],
                    'cell' => $cell
                );
                array_push($result, $item);
                $i++;
            }

        $pagesCount = ceil($totalItemsPerSearch / $items_per_page);

        $data = array(
            'total' => $pagesCount,
            'page' => $pagenum,
            'records' => $i,
            'rows' => $result
        );

        return $data;
    }
    
    function furnitureProcess($params = array()){

        if(count($params) == 0){

            $tmp = $this->dismount($this->registry->request);

            if(isset($tmp['get']['page'])) $params['offset'] = $tmp['get']['page'];
            if(isset($tmp['post']['page'])) $params['offset'] = $tmp['post']['page'];

            if(isset($tmp['get']['rows'])) $params['limit'] = $tmp['get']['rows'];
            if(isset($tmp['post']['rows'])) $params['limit'] = $tmp['post']['rows'];

            if(isset($tmp['get']['sidx'])) $params['order_by_column'] = $tmp['get']['sidx'];
            if(isset($tmp['post']['sidx'])) $params['order_by_column'] = $tmp['post']['sidx'];

            if(isset($tmp['get']['sord'])) $params['order'] = $tmp['get']['sord'];
            if(isset($tmp['post']['sord'])) $params['order'] = $tmp['post']['sord'];

            unset($tmp);
        }

        # search
        $search = (string) $this->registry->request->getParameter('_search');
        $searchField = (string) $this->registry->request->getParameter('searchField');
        $searchOper = (string) $this->registry->request->getParameter('searchOper');
        $searchString = (string) $this->registry->request->getParameter('searchString');

        $search_params = array();
        $search_params['search'] = $search;
        $search_params['searchField'] = $searchField;
        $search_params['searchOper'] = $searchOper;
        $search_params['searchString'] = $searchString;


        # count
        $whole_count = 0;

        $PhotoModel = new PhotoModel('vmodul_id',$this -> registry -> virtual_modul_id );
        $data = $PhotoModel -> getData();

        if(is_array($data)) $whole_count = count($data);

        unset($PhotoModel);
        unset($data);

        if($search === 'true'){
            $PhotoModel = new PhotoModel();
            $PhotoModel->getPhotoVmodul($this -> registry -> virtual_modul_id, $search_params);

        } else {
            //$PhotoModel = new PhotoModel('vmodul_id',Registry::getInstance() -> virtual_modul_id );
            $PhotoModel = new PhotoModel('vmodul_id',$this -> registry -> virtual_modul_id, $params );
        }

        $data = $PhotoModel -> getData();

        if( is_array($data) ){
            if(count($data) > 0){
                $this->dataPostProcess($data);
                //$this->setData($data);
            }
        }

        $photos['whole_count'] = $whole_count;
        $photos['count'] = is_array($data) ?  count($data) : 0;
        $photos['data']  = $data;

        return $photos;

    }

    private function dataPostProcess(&$data){
        for($i = 0; $i < count($data); $i++){
            $link = $data[$i]['link'];
            $data[$i]['link'] =    "$link";
            $data[$i]['link_sm'] =  "sm_$link";
        }
    }
    
}